package com.woniuxy.converter;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringToDateConverter implements Converter<String,Date> {
   private String pattern;

    public StringToDateConverter(String pattern) {
        this.pattern = pattern;
    }
    @Override
    public Date convert(String source) {
        Date date=null;
        if(source==null){
            throw new IllegalArgumentException("请输入参数");
        }
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(pattern);
        try {
            date=simpleDateFormat.parse(source);
        } catch (ParseException e) {
            throw new RuntimeException("类型转换异常");
        }
        return date;
    }
}
